﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace TouchUI.System.Mobile.Mvc
{
  public class ImageButton : UserControl
  {   

    //Private members      
    private Image image;
    //flag to indicate the pressed state
    private bool bPushed;
    private Bitmap m_bmpOffscreen;

    public ImageButton()
    {
      bPushed = false;
      //default minimal size
      this.Size = new Size(21, 21);
    }

    public Image Image
    {
      get
      {
        return image;
      }
      set
      {
        image = value;
      }
    }

    private Color BackgroundImageColor(Image image)
    {
      Bitmap bmp = new Bitmap(image);
      return bmp.GetPixel(0, 0);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics gxOff;      //Offscreen graphics
      Rectangle imgRect; //image rectangle
      Brush backBrush;   //brush for filling a backcolor

      if (m_bmpOffscreen == null) //Bitmap for doublebuffering
      {
        m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
      }

      gxOff = Graphics.FromImage(m_bmpOffscreen);

      gxOff.Clear(this.BackColor);

      if (!bPushed)
        backBrush = new SolidBrush(Parent.BackColor);
      else //change the background when it's pressed
        backBrush = new SolidBrush(Color.LightGray);

      gxOff.FillRectangle(backBrush, this.ClientRectangle);

      if (image != null)
      {
        //Center the image relativelly to the control
        int imageLeft = (this.Width - image.Width) / 2;
        int imageTop = (this.Height - image.Height) / 2;

        if (!bPushed)
          imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
        else //The button was pressed
          //Shift the image by one pixel
          imgRect = new Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height);

        //Set transparent key
        ImageAttributes imageAttr = new ImageAttributes();
        imageAttr.SetColorKey(BackgroundImageColor(image),
                                              BackgroundImageColor(image));
        //Draw image
        //gxOff.DrawImage(image, imgRect, new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
        gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height,
                                            GraphicsUnit.Pixel, imageAttr);

      }

      if (bPushed) //The button was pressed
      {
        //Prepare rectangle
        Rectangle rc = this.ClientRectangle;
        rc.Width--;
        rc.Height--;
        //Draw rectangle
        gxOff.DrawRectangle(new Pen(Color.Gray), rc);
      }

      //Draw from the memory bitmap
      e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);

      base.OnPaint(e);
    }


    protected override void OnMouseDown(MouseEventArgs e)
    {
      bPushed = true;
      this.Invalidate();     
      //OnClick(EventArgs.Empty);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
      bPushed = false;
      this.Invalidate();
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
      //Do nothing
    }
  }
}